﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.ServiceModel;
using System.Web;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using WmsService.BusinessLogic;
using WmsService.Security;

namespace WmsService.AOP
{
    public class InvoiceBillInStoreBillHistoryCallHandler : ICallHandler
    {
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            //开始获取实体
            string inStoreBillID= null;
            string invoiceNo = null;
            InvoiceBillInStoreBill invoiceBillInStoreBill = null;

            if(input.Arguments.Count != 2)
            {
                throw new AopException("[InvoiceBillInStoreBillHistoryCallHandler] 特性绑定方法错误：" + input.MethodBase.Name);
            }

            invoiceNo = (String)input.Arguments[0];
            inStoreBillID = (String)input.Arguments[1]; 
           

            //var logic = (IInvoiceAndPayApplyBillLogic)input.Target;
            dynamic logic = input.Target;
            invoiceBillInStoreBill = logic.GetInvoiceBillInStoreBill(invoiceNo, inStoreBillID);
            
            //执行方法，如果没有异常，就执行下面的保存历史记录方法
            InvokeHandlerDelegate invokeHandlerDelegate = getNext();
            var methodReturn = invokeHandlerDelegate(input, getNext);

            var invoiceBillInStoreBillHistory = InvoiceBillInStoreBillHistory.
                CreateInvoiceBillInStoreBillHistory(0, 
                invoiceBillInStoreBill.InvoiceNo,
                invoiceBillInStoreBill.InStoreBillID, 
                invoiceBillInStoreBill.Tag,
                invoiceBillInStoreBill.PayAmountTax);

            var propertyInfos = invoiceBillInStoreBill.GetType().GetProperties();

            PropertyInfo pi;
            foreach (var propertyInfo in propertyInfos)
            {
                pi = typeof(InvoiceBillInStoreBillHistory).GetProperty(propertyInfo.Name);
                if(pi != null && pi.Name != "EntityKey")
                {
                    try
                    {
                        pi.SetValue(invoiceBillInStoreBillHistory, propertyInfo.GetValue(invoiceBillInStoreBill, null), null);
                    }
                    catch (Exception)
                    {}
                }
            }

            invoiceBillInStoreBillHistory.OperationTime = DateTime.Now;
            invoiceBillInStoreBillHistory.ExtVarChar3 = "删除该记录";
            var logicBase = (LogicBase)input.Target;
            invoiceBillInStoreBillHistory.ExtVarChar4 = logicBase.ContextUserID;

            var ctx = Factory.ContextFactory.Create();
            ctx.AddToInvoiceBillInStoreBillHistory(invoiceBillInStoreBillHistory);
            ctx.SaveChanges();

            return methodReturn;
        }
        public int Order { get; set; }
    }

    public class InvoiceBillInStoreBillHistoryCallHandlerAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new InvoiceBillInStoreBillHistoryCallHandler { Order = this.Order };
        }
    }
}